package com.magicsoft.share.ui.my;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.adapter.my.RecordGoldAdapter;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.my.GoldDetail;
import com.magicsoft.share.presenter.my.GoldPresenter;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.view.my.IGoldView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import butterknife.BindView;
import butterknife.OnClick;

public class GoldActivity extends BaseActivity<IGoldView, GoldPresenter> implements IGoldView {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;
    private RecordGoldAdapter recordGoldAdapter;
    private String goldNum;
    private int pageNo = 1;
    private int pageSize = 10;
    private boolean isRefresh = true;

    @Override
    public GoldPresenter initPresenter() {
        return new GoldPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_gold;
    }

    @Override
    public void initData() {

        goldNum = getIntent().getStringExtra("goldNum");
    }

    @Override
    public void initView() {
        tvTitle.setText("金币");
        tvNum.setText(goldNum);

        initRV();
    }


    @Override
    public void requestData() {

        mPresenter.myAmountDetail(pageNo, pageSize);
    }


    @OnClick(R.id.ibtn_left)
    public void onViewClicked() {
        finish();
    }

    private void initRV() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg), 0, DensityUtils.dp2px(this, 0.5f)));
        recyclerView.useDefaultLoadMore();
        recyclerView.loadMoreFinish(true, false);
        recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {

                isRefresh = false;
                pageNo++;
                requestData();
            }
        });

        recordGoldAdapter = new RecordGoldAdapter(R.layout.item_record_gold);

        recyclerView.setAdapter(recordGoldAdapter);

    }

    @Override
    public void myAmountDetailSuccess(GoldDetail data) {
        if (isRefresh) {
            recordGoldAdapter.setNewData(data.getList());
        } else {
            recordGoldAdapter.addData(data.getList());
        }

        if (pageNo < data.getPages()) {
            recyclerView.loadMoreFinish(false, true);
        } else if (pageNo==data.getPages()){
            recyclerView.loadMoreFinish(false, false);
        }else {
            recyclerView.loadMoreFinish(true, false);
        }
    }

    @Override
    public void myAmountDetailFail() {
        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }
}
